home *** CD-ROM | disk | FTP | other *** search
/ C & C++ Multimedia Cyber Classroom / C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso / src / fig20_31.jar / Ch20 / Fig20_31 / fig20_31.cpp
C/C++ Source or Header  |  1997-10-22  |  2KB  |  66 lines

  1. // Fig. 20.31: fig20_31.cpp
  2. // Demonstrates search and sort capabilities.
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. bool greater10( int value );
  10.  
  11. int main()
  12. {
  13.    const int SIZE = 10;
  14.    int a[ SIZE ] = { 10, 2, 17, 5, 16, 8, 13, 11, 20, 7 };
  15.    vector< int > v( a, a + SIZE );
  16.    ostream_iterator< int > output( cout, " " );
  17.  
  18.    cout << "Vector v contains: ";
  19.    copy( v.begin(), v.end(), output );
  20.    
  21.    vector< int >::iterator location;
  22.    location = find( v.begin(), v.end(), 16 );
  23.  
  24.    if ( location != v.end() ) 
  25.       cout << "\n\nFound 16 at location " 
  26.            << ( location - v.begin() );
  27.    else 
  28.       cout << "\n\n16 not found";
  29.    
  30.    location = find( v.begin(), v.end(), 100 );
  31.  
  32.    if ( location != v.end() ) 
  33.       cout << "\nFound 100 at location " 
  34.            << ( location - v.begin() );
  35.    else 
  36.       cout << "\n100 not found";
  37.  
  38.    location = find_if( v.begin(), v.end(), greater10 );
  39.  
  40.    if ( location != v.end() ) 
  41.       cout << "\n\nThe first value greater than 10 is "
  42.            << *location << "\nfound at location " 
  43.            << ( location - v.begin() );
  44.    else 
  45.       cout << "\n\nNo values greater than 10 were found";
  46.  
  47.    sort( v.begin(), v.end() );
  48.    cout << "\n\nVector v after sort: ";
  49.    copy( v.begin(), v.end(), output );
  50.  
  51.    if ( binary_search( v.begin(), v.end(), 13 ) )
  52.       cout << "\n\n13 was found in v";
  53.    else
  54.       cout << "\n\n13 was not found in v";
  55.  
  56.    if ( binary_search( v.begin(), v.end(), 100 ) )
  57.       cout << "\n100 was found in v";
  58.    else
  59.       cout << "\n100 was not found in v";
  60.  
  61.    cout << endl;
  62.    return 0;
  63. }
  64.  
  65. bool greater10( int value ) { return value > 10; }
  66.